package com.example.demo.security.sysMessage.controller;


import com.example.demo.db.MessageBean;
import com.example.demo.security.sysMessage.entity.MessagesBean;
import com.example.demo.security.sysMessage.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @RequestMapping("toMessage")
    public String toMessage(){
        return "security/message";
    }

    /**
     * 获取好友消息列表
     * @author 自嘲
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("friendList")
    @ResponseBody
    public List<MessagesBean> friendList(HttpServletRequest request, Integer id){
        Integer userId=Integer.parseInt(request.getSession().getAttribute("id").toString());
        List<MessagesBean> list;
        try {
            list=messageService.queryFriendMessage(userId,id);
        }catch (Exception e){
            list=null;
        }
        return list;
    }

    @RequestMapping("groupList")
    @ResponseBody
    public List<MessagesBean> groupList(HttpServletRequest request, Integer id){
        List<MessagesBean> list;
        try {
            list=messageService.queryGroupMessage(id);
        }catch (Exception e){
            list=null;
        }
        return list;
    }

    @RequestMapping("sendMessage")
    @ResponseBody
    public String sendMessage(MessagesBean messagesBean,Integer state){
        try {
            Integer i=messageService.insertMessage(messagesBean,state);
            if (i==0){
                return "发送失败！";
            }else {
                return "发送成功！";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "系统异常！";
        }
    }
}
